package sfin.swt.composite;

import java.io.IOException;
import java.util.List;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import sfin.swt.util.ApplicationUtil;
import sfin.swt.util.IconCredit;
import sfin.swt.util.ResourceUtil;

public class AboutComposite extends Composite{

	private ResourceBundle bundle;
	private ApplicationUtil applicationUtil;
	private TabFolder folder;
	
	public AboutComposite(Composite parent, int style, ResourceBundle bundle, ApplicationUtil applicationUtil) {
		super(parent, style);
		this.bundle = bundle;
		this.applicationUtil = applicationUtil;

		try {
			initializeVersionFolder();
			initializeCreditsFolder();
		} catch (IOException e) {
			MessageBox message = new MessageBox(getShell(), SWT.ERROR);
			message.setText(e.getLocalizedMessage());
			message.setMessage(e.getMessage());
			message.open();
		}
	}

	private void initializeCreditsFolder() throws IOException {
		
		TabItem tabItem = new TabItem(folder, SWT.NONE);
		tabItem.setText(bundle.getString("help.about.credits"));
		
		final ScrolledComposite sc = new ScrolledComposite(folder, SWT.V_SCROLL | SWT.H_SCROLL);
		
		tabItem.setControl(sc);
		
		final Group composite = new Group(sc, SWT.NONE);
		composite.setText(bundle.getString("help.about.credits.images.title"));
		composite.setLayout(new GridLayout());
		
		sc.setContent(composite);
		sc.setExpandHorizontal(true);
		sc.setExpandVertical(true);
		
		sc.addControlListener(new ControlAdapter() {
	            public void controlResized(ControlEvent e) {
	            	Rectangle r = sc.getClientArea();
	                sc.setMinSize(folder.computeSize(r.width, r.height + 450));
	            }
	        });

		List<IconCredit> credits = ResourceUtil.getIconCredits();
				for(int i = 0; i<credits.size(); i++){
					plotCredit(credits.get(i), composite, i == credits.size() - 1);
		}
				
	}
	
	protected void plotCredit(IconCredit iconCredit, Composite composite, boolean last){
		Label label = new Label(composite, SWT.NONE);
		label.setText(iconCredit.getName());
		
		GridData gridData = new GridData();
		label.setLayoutData(gridData);
		
		Link link = new Link(composite, SWT.NONE);
		link.setText("<a>" + iconCredit.getLink() + "</a>");
		link.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Program.launch(e.text);
			}
		});
		
		gridData = new GridData();
		link.setLayoutData(gridData);
		
		label = new Label(composite, SWT.NONE);
		label.setText(iconCredit.getResources());
		
		gridData = new GridData();
		label.setLayoutData(gridData);
		
		if(!last){
			label = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
			gridData = new GridData(GridData.FILL_HORIZONTAL);
			gridData.grabExcessHorizontalSpace = true;
			label.setLayoutData(gridData);
		}
		
	}
	
	private void initializeVersionFolder() {

		setLayout(new GridLayout());
		folder = new TabFolder(this, SWT.NONE);
		folder.setLayout(new FillLayout());
		
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
		folder.setLayoutData(gridData);

		Composite composite = new Composite(folder, SWT.NONE);
		composite.setLayout(new GridLayout(1,true));
		
		TabItem tabItem = new TabItem(folder, SWT.NONE);
		tabItem.setControl(composite);
		tabItem.setText(bundle.getString("help.about.title"));
	
		Label label = new Label(composite, SWT.NONE);
		label.setAlignment(SWT.CENTER);
		label.setImage(ResourceUtil.getImage(getDisplay(), "logotipo.png"));
		
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.heightHint = 250;
				
		label.setLayoutData(gridData);
		
		label = new Label(composite, SWT.NONE);
		label.setText(applicationUtil.getName() + " " +applicationUtil.getVersion());
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		label.setLayoutData(gridData);

		label = new Label(composite, SWT.NONE);
		label.setText(applicationUtil.getArch() );
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		label.setLayoutData(gridData);

		Link link = new Link(composite, SWT.NONE);
		link.setText("<a>" + bundle.getString("licence.terms.label") + "</a>");
		link.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Program.launch(applicationUtil.get(ApplicationUtil.LICENCE_LINK));
			}
		});
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		link.setLayoutData(gridData);

		label = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		label.setLayoutData(gridData);

		label = new Label(composite, SWT.NONE);
		label.setText( bundle.getString("about.developer.title"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		label.setLayoutData(gridData);

		label = new Label(composite, SWT.NONE);
		label.setText( bundle.getString("developer.name"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		label.setLayoutData(gridData);
		
		label = new Label(composite, SWT.NONE);
		label.setText( bundle.getString("developer.school"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		label.setLayoutData(gridData);

		label = new Label(composite, SWT.NONE);
		label.setText( bundle.getString("developer.job"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		label.setLayoutData(gridData);
		
		
	}
}
